﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Register.Model;
using Register.BLL;
using Register.DAL;
using Register.Command;
using System.Web.Script.Serialization;
using System.Text;
namespace RegisterWeb.Manager.DoctorPersonalCenter.ashx
{
    /// <summary>
    /// OutpatientHandler 的摘要说明
    /// </summary>
    public class OutpatientHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request["action"];
            if (action.Equals("get"))
            {
                int row = int.Parse(context.Request["rows"].ToString());
                int page = int.Parse(context.Request["page"].ToString());
                List<RegisterInfo> list = new List<RegisterInfo>();
                list = OutpatientBLL.GetPagedOutpatientInfo(row, page, "  Appointment_ID in (select Appointment_ID from  System_appointment where Doctor_ID=\'DO20120709002 \' ) ");  //临时模拟医生

                List<OutpatientJSON> json = new List<OutpatientJSON>();
                foreach (RegisterInfo reg in list)
                {
                    //此处医生编号为人为复制，实际从session中获取
                    string OutpatientTypeValue = OutpatientBLL.GetOutpatientTypeById("DO20120709002");

                    string isClinic = "<img src='../../images/user_cross.png' />未就诊";
                    if (reg.Register_IsClinic.Equals("1"))
                    {
                        isClinic = "<img src='../../images/user_tick.png' />已就诊";

                    }


                    json.Add(new OutpatientJSON(reg.Register_ID, Convert.ToString(reg.Register_Number), reg.Register_IDCode, reg.Register_PatientName, reg.Register_PaintSex, Convert.ToString(reg.Register_BirthDay).Substring(0, 9), reg.Register_IDCards, isClinic, OutpatientTypeValue, HospitalInfoBLL.GetParamNameById(OutpatientTypeValue)));
                }

                OutpatientListJSON patientListJSON = new OutpatientListJSON();
                patientListJSON.total = OutpatientBLL.GetOutpatientInfoCount("  Appointment_ID in (select Appointment_ID from  System_appointment where Doctor_ID=\'DU20120709002\' ) ");
                patientListJSON.rows = json;

                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(patientListJSON));
            }
            #region 添加患者就诊信息
            else if (action.Equals("addOutpatient"))
            {


                Outpatient patient = new Outpatient();

                patient.Outpatient_ID = DataIDHelper.GetDataID("Outpatient_ID");
                patient.Outpatient_AddTime = DateTime.Now;
                patient.Outpatient_IsClinic = "1";
                patient.Outpatient_MainIntroduce = context.Request["Outpatient_MainIntroduce"];
                patient.Outpatient_OutpaitentType = context.Request["Outpatient_OutpaitentType"];
                patient.Outpatient_PaitentBirthDay = Convert.ToDateTime(context.Request["Outpatient_PaitentBirthDay"]);
                patient.Outpatient_PaitentIDCards = context.Request["Outpatient_PaitentIDCards"];
                patient.Outpatient_PaitentName = context.Request["Outpatient_PaitentName"];
                patient.Outpatient_PaitentSex = context.Request["Outpatient_PaitentSex"];
                patient.Outpatient_PastHistory = context.Request["Outpatient_PastHistory"];
                patient.Outpatient_Recoder = "DoubleZ";
                patient.Outpatient_ShowHistory = context.Request["Outpatient_ShowHistory"];
                patient.Register_ID = context.Request["id"];
                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(new message(OutpatientBLL.AddOutpatientInfo(patient), "患者就诊信息添加成功！")));
            }
            #endregion
            #region 绑定性别
            else if (action.Equals("gender"))
            {
                context.Response.Write("[{\"Gender_ID\":\"男\",\"Gender_Name\":\"男\"},{\"Gender_ID\":\"女\",\"Gender_Name\":\"女\"}]");
            }
            #endregion
            #region 绑定门诊类型
            else if (action.Equals("outpatientType"))
            {
                List<Parameter> list;
                JavaScriptSerializer jss = new JavaScriptSerializer();
                list = ParamterHelper.GetParametersByType("regType");
                context.Response.Write(jss.Serialize(list));

            }
            #endregion
            #region 检查患者是否已就诊
            else if (action.Equals("checkPatient"))
            {
                string registerId = context.Request["id"];
                if (OutpatientBLL.Exists(registerId))
                {
                    context.Response.Write("ok");
                }
                else
                {
                    context.Response.Write("no");
                }
            }
            #endregion
            #region 查看患者就诊信息
            else  if(action.Equals("seeOutpatient")){
                  string id=context.Request["id"];
                  List<Outpatient> list = new List<Outpatient>();
                  list = OutpatientBLL.GetOutpatientInfo(id);
                   List<patientJSON> json=new List<patientJSON>();
                 foreach (Outpatient pat in list)
                  {
                  json.Add(new patientJSON(Convert.ToString(pat.Outpatient_AddTime),pat.Outpatient_MainIntroduce,HospitalInfoBLL.GetParamNameById(pat.Outpatient_OutpaitentType),Convert.ToString(pat.Outpatient_PaitentBirthDay).Substring(0,9),pat.Outpatient_PaitentIDCards,pat.Outpatient_PaitentName,pat.Outpatient_PaitentSex,pat.Outpatient_PastHistory,pat.Outpatient_Recoder,pat.Outpatient_ShowHistory,pat.Outpatient_IsClinic,pat.Outpatient_ID,pat.Register_ID));
                  JavaScriptSerializer jss = new JavaScriptSerializer();
                   context.Response.Write(jss.Serialize(json));   
                 }

               //  context.Response.Write("{\"Outpatient_PaitentName\":\"doublez\"}");   

            }
                
            
                #endregion
            #region 多条件查询
            else if (action.Equals("search")) { 
               string outpatientTimeFrom = context.Request["outpatientTimeFrom"];

               string outpatientTimeTo = context.Request["outpatientTimeTo"];

                string patientName= context.Request["patientName"];


                int row = int.Parse(context.Request["rows"].ToString());
                int page = int.Parse(context.Request["page"].ToString());

                StringBuilder strBuilder = new StringBuilder(" Outpatient_IsClinic=1 ");
               // strBuilder.Append(" Departments_State='1' ");
                if (!String.IsNullOrEmpty(patientName)){
                    strBuilder.Append(" and Outpatient_PaitentName='").Append(patientName).Append("' ");
                }
                if (!String.IsNullOrEmpty(outpatientTimeFrom))
                {
                   
                        strBuilder.Append("  and Outpatient_AddTime between '").Append(outpatientTimeFrom).Append("' and '");
                 
                }
                if (!String.IsNullOrEmpty(outpatientTimeTo))
                {
                    strBuilder.Append(outpatientTimeTo).Append("' ");
                }
               else { strBuilder.Append(outpatientTimeFrom).Append("' "); }
                
                  List<Outpatient> list = new List<Outpatient>();
                  list =  OutpatientBLL.GetPagedSearchInfo(row,page,strBuilder.ToString());


                List<patientJSON> json = new List<patientJSON>();

                 foreach (Outpatient pat in list)
                {
                    //此处医生编号为人为复制，实际从session中获取
                    //string OutpatientTypeValue = OutpatientBLL.GetOutpatientTypeById("DU20120709002")
                    
                    string isClinic = "<img src='../../images/user_tick.png' />已就诊";
                    if (pat.Outpatient_IsClinic.Equals("0"))
                    {
                         isClinic = "<img src='../../images/user_cross.png' />未就诊";

                    }
               json.Add(new patientJSON(Convert.ToString(pat.Outpatient_AddTime),pat.Outpatient_MainIntroduce,HospitalInfoBLL.GetParamNameById(pat.Outpatient_OutpaitentType),Convert.ToString(pat.Outpatient_PaitentBirthDay).Substring(0,9),pat.Outpatient_PaitentIDCards,pat.Outpatient_PaitentName,pat.Outpatient_PaitentSex,pat.Outpatient_PastHistory,pat.Outpatient_Recoder,pat.Outpatient_ShowHistory,isClinic,pat.Outpatient_ID,pat.Register_ID));
         
                 }
                 patientListJSON patientListJSON=new patientListJSON();
                patientListJSON.total = OutpatientBLL.GetSearchCount(strBuilder.ToString());
                patientListJSON.rows = json;


                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(patientListJSON));
            
            
            }
            #endregion

        }
        
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
class OutpatientListJSON
{

    public int total { get; set; }
    public List<OutpatientJSON> rows { get; set; }

}
class OutpatientJSON
{
    public string Register_ID
    {
        set;
        get;
    }
    public string Register_Number
    {
        set;
        get;
    }
    public string Register_IDCode
    {
        get;
        set;
    }
    public string Register_PatientName
    {
        get;
        set;
    }

    public string Register_PaintSex
    {
        get;
        set;
    }
    public string Register_BirthDay
    {
        get;
        set;
    }
    public string Register_IDCards
    {
        get;
        set;
    }

    public string Register_IsClinic
    {
        get;
        set;
    }
    public string OutpatientTypeValue
    {
        get;
        set;
    }
    public string OutpatientType
    {
        get;
        set;
    }

    public OutpatientJSON(string Register_ID, string Register_Number, string Register_IDCode, string Register_PatientName, string Register_PaintSex, string Register_BirthDay, string Register_IDCards, string Register_IsClinic, string OutpatientTypeValue, string OutpatientType)
    {
        this.Register_BirthDay = Register_BirthDay;
        this.Register_ID = Register_ID;
        this.Register_IDCards = Register_IDCards;
        this.Register_IDCode = Register_IDCode;
        this.Register_IsClinic = Register_IsClinic;
        this.Register_Number = Register_Number;
        this.Register_PaintSex = Register_PaintSex;
        this.Register_PatientName = Register_PatientName;
        this.OutpatientTypeValue = OutpatientTypeValue;
        this.OutpatientType = OutpatientType;
    }
}

class patientListJSON
{

    public int total { get; set; }
    public List<patientJSON> rows { get; set; }

}
class patientJSON
{

    public string Outpatient_PaitentName
    {
        set;
        get;
    }
    public string Outpatient_PaitentSex
    {
        get;
        set;
    }
    public string Outpatient_PaitentBirthDay
    {
        get;
        set;
    }

    public string Outpatient_AddTime
    {
        get;
        set;
    }
    public string Outpatient_MainIntroduce
    {
        get;
        set;
    }
    public string Outpatient_OutpaitentType
    {
        get;
        set;
    }

    public string Outpatient_PaitentIDCards
    {
        get;
        set;
    }
    public string Outpatient_PastHistory
    {
        get;
        set;
    }
    public string Outpatient_Recoder
    {
        get;
        set;
    }
    public string Outpatient_ShowHistory{
    get;
    set;
    
    }
    public string Outpatient_IsClinic{get;set;}
    public string Outpatient_ID{get;set;}
    public string Register_ID{get;set;}
    public patientJSON(string Outpatient_AddTime,string Outpatient_MainIntroduce,string Outpatient_OutpaitentType,string Outpatient_PaitentBirthDay,string Outpatient_PaitentIDCards,string Outpatient_PaitentName,string Outpatient_PaitentSex,string Outpatient_PastHistory,string Outpatient_Recoder,string Outpatient_ShowHistory,string Outpatient_IsClinic,string Outpatient_ID,string Register_ID)
    {
        this.Outpatient_AddTime = Outpatient_AddTime;
        this.Outpatient_MainIntroduce = Outpatient_MainIntroduce;
        this.Outpatient_OutpaitentType = Outpatient_OutpaitentType;
        this.Outpatient_PaitentBirthDay = Outpatient_PaitentBirthDay;
        this.Outpatient_PaitentIDCards = Outpatient_PaitentIDCards;
        this.Outpatient_PaitentName = Outpatient_PaitentName;
        this.Outpatient_PaitentSex = Outpatient_PaitentSex;
        this.Outpatient_PastHistory = Outpatient_PastHistory;
        this.Outpatient_Recoder = Outpatient_Recoder;
        this.Outpatient_ShowHistory = Outpatient_ShowHistory;
        this.Outpatient_IsClinic=Outpatient_IsClinic;
        this.Outpatient_ID=Outpatient_ID;
        this.Register_ID=Register_ID;
    }
}